package com.ruoyi.common.utils;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * WXshiro 工具类
 *
 * @author ruoyi
 */

public class WXShiroUtils {

    public static Map<String, Object> getSubject(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> wxLoginCache = WXLoginUtils.getWXLoginCache(token);
        return wxLoginCache;
    }

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static Long getUserId() {
        SysUser user = getSysUser();
        return user.getUserId();
    }

    public static Session getSession() {
        return WXShiroUtils.getSubject().getSession();
    }

    public static Long getCompanyId() {
        SysUser user = getSysUser();
        return user.getCompanyId();
    }

    public static String getLoginName() {
        SysUser user = getSysUser();
        return user.getUserName();
    }

    public static String getToken() {
        String token = ContextHolderUtils.getToken();
        return token;
    }

    public static SysUser getSysUser() {
        String source = ContextHolderUtils.getSource();
        SysUser user = null;
        switch (source) {
            case "1":
                HttpServletRequest request = ContextHolderUtils.getRequest();
                String token = request.getHeader("token");
                Map<String, Object> wxLoginCache = WXLoginUtils.getWXLoginCache(token);
                if (wxLoginCache != null) {
                    user = (SysUser) wxLoginCache.get("user");
                    return user;
                } else {
                    return user;
                }
            case "2":
            default:
                user = ShiroUtils.getSysUser();
                return user;
        }
    }

    public static Long getCompanyId(HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> wxLoginCache = WXLoginUtils.getWXLoginCache(token);
        SysUser user = (SysUser) wxLoginCache.get("user");
        Long companyId = user.getCompanyId();
        return companyId;
    }

    public static String getUserName() {
        SysUser sysUser = getSysUser();
        return sysUser.getUserName();
    }

    public static class ContextHolderUtils {
        public static HttpServletRequest getRequest() {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            return request;
        }

        public static String getSource() {
            HttpServletRequest request = getRequest();
            String source = request.getHeader("source");
            if (source != null) {
                return source;
            }
            return "0";
        }

        public static String getToken() {
            HttpServletRequest request = getRequest();
            String source = request.getHeader("token");
            return source;
        }
    }

}
